using System;
using Cmpp.Client.Cmpp3Message.Utility;

namespace Cmpp.Client.Cmpp3Message {
    public abstract class Cmpp3Request {
        private readonly Cmpp3Header m_header;

        protected Cmpp3Request(Cmpp3Command command) {
            m_header = new Cmpp3Header(command);
        }

        public Cmpp3Header Header {
            get {
                return m_header;
            }
        }

        public byte[] ToBytes() {
            var writer = new CmppMessageWriter();
            m_header.Write(writer);
            WriteBody(writer);

            byte[] byteRequest = writer.ToBytes();
            byte[] byteLength = DataConvert.UInt32ToBytes((uint)byteRequest.Length);
            Buffer.BlockCopy(byteLength, 0, byteRequest, 0, 4);
            
            return byteRequest;
        }

        public override string ToString() {
            byte[] request = ToBytes();
            return BitConverter.ToString(request, 0, request.Length);
        }

        protected abstract void WriteBody(CmppMessageWriter writer);
    }
}